package com.mmg.spzx.manager.controller;

import com.mmg.spzx.manager.service.CategoryService;
import com.mmg.spzx.model.entity.product.Category;
import com.mmg.spzx.model.vo.common.Result;
import com.mmg.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "商品分类管理")
@RestController
@RequestMapping("/admin/product/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "导入数据")
    @PostMapping("/importData")
    public Result importData(MultipartFile file) {
        //判断文件是否为空
        if (file.isEmpty()) {
            return Result.build(null, ResultCodeEnum.DATA_ERROR);
        }
        //判断文件类型
        if (!file.getOriginalFilename().endsWith(".xlsx")) {
            return Result.build(null, ResultCodeEnum.DATA_ERROR);
        }
        categoryService.importData(file);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "导出数据")
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response) {
        categoryService.exportData(response);
    }

    @Operation(summary = "根据parent_id获取商品分类列表")
    @GetMapping("/findCategoryList/{id}")
    public Result findCategoryList(@PathVariable("id") Long id) {
        List<Category> categoryList = categoryService.findCategoryList(id);
        return Result.build(categoryList, ResultCodeEnum.SUCCESS);
    }
}
